<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="bltin-file-objects.html" />
<link rel="prev" href="types-set.html" />
<link rel="parent" href="types.html" />
<link rel="next" href="bltin-file-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>3.8 Mapping Types -- dict </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.7 set Types "
  href="types-set.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="3. built-in Types"
  href="types.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="3.9 file Objects"
  href="bltin-file-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="types-set.html">3.7 Set Types </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="types.html">3. Built-in Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bltin-file-objects.html">3.9 File Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION005800000000000000000"></a><a name="typesmapping"></a>
<br>
3.8 Mapping Types -- <tt class="class">dict</tt> 
</h1>
<a id='l2h-287' xml:id='l2h-287'></a><a id='l2h-288' xml:id='l2h-288'></a>
<p>
A <i class="dfn">mapping</i> object maps  immutable values to
arbitrary objects.  Mappings are mutable objects.  There is currently
only one standard mapping type, the <i class="dfn">dictionary</i>.  A dictionary's keys are
almost arbitrary values.  Only values containing lists, dictionaries
or other mutable types (that are compared by value rather than by
object identity) may not be used as keys.
Numeric types used for keys obey the normal rules for numeric
comparison: if two numbers compare equal (such as <code>1</code> and
<code>1.0</code>) then they can be used interchangeably to index the same
dictionary entry.

<p>
Dictionaries are created by placing a comma-separated list of
<code><var>key</var>: <var>value</var></code> pairs within braces, for example:
<code>{'jack': 4098, 'sjoerd': 4127}</code> or
<code>{4098: 'jack', 4127: 'sjoerd'}</code>.

<p>
The following operations are defined on mappings (where <var>a</var> and
<var>b</var> are mappings, <var>k</var> is a key, and <var>v</var> and <var>x</var> are
arbitrary objects):
<a id='l2h-289' xml:id='l2h-289'></a><a id='l2h-290' xml:id='l2h-290'></a><a id='l2h-291' xml:id='l2h-291'></a><a id='l2h-292' xml:id='l2h-292'></a><a id='l2h-294' xml:id='l2h-294'></a>
<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">Operation</th>
      <th class="left"  >Result</th>
      <th class="center">Notes</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code>len(<var>a</var>)</code></td>
        <td class="left"  >the number of items in <var>a</var></td>
        <td class="center"></td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>[<var>k</var>]</code></td>
        <td class="left"  >the item of <var>a</var> with key <var>k</var></td>
        <td class="center">(1), (10)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>[<var>k</var>] = <var>v</var></code></td>
        <td class="left"  >set <code><var>a</var>[<var>k</var>]</code> to <var>v</var></td>
        <td class="center"></td></tr>
    <tr><td class="center" valign="baseline"><code>del <var>a</var>[<var>k</var>]</code></td>
        <td class="left"  >remove <code><var>a</var>[<var>k</var>]</code> from <var>a</var></td>
        <td class="center">(1)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.clear()</code></td>
        <td class="left"  >remove all items from <code>a</code></td>
        <td class="center"></td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.copy()</code></td>
        <td class="left"  >a (shallow) copy of <code>a</code></td>
        <td class="center"></td></tr>
    <tr><td class="center" valign="baseline"><code><var>k</var> in <var>a</var></code></td>
        <td class="left"  ><code>True</code> if <var>a</var> has a key <var>k</var>, else <code>False</code></td>
        <td class="center">(2)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>k</var> not in <var>a</var></code></td>
        <td class="left"  >Equivalent to <code>not</code> <var>k</var> in <var>a</var></td>
        <td class="center">(2)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.has_key(<var>k</var>)</code></td>
        <td class="left"  >Equivalent to <var>k</var> <code>in</code> <var>a</var>, use that form in new code</td>
        <td class="center"></td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.items()</code></td>
        <td class="left"  >a copy of <var>a</var>'s list of (<var>key</var>, <var>value</var>) pairs</td>
        <td class="center">(3)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.keys()</code></td>
        <td class="left"  >a copy of <var>a</var>'s list of keys</td>
        <td class="center">(3)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.update(<big>[</big><var>b</var><big>]</big>)</code></td>
        <td class="left"  >updates <var>a</var> with key/value pairs from <var>b</var>, overwriting
	   existing keys, returns <code>None</code></td>
        <td class="center">(9)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.fromkeys(<var>seq</var><big>[</big>, <var>value</var><big>]</big>)</code></td>
        <td class="left"  >Creates a new dictionary with keys from <var>seq</var> and values set to <var>value</var></td>
        <td class="center">(7)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.values()</code></td>
        <td class="left"  >a copy of <var>a</var>'s list of values</td>
        <td class="center">(3)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.get(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td>
        <td class="left"  ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>,
           else <var>x</var></td>
        <td class="center">(4)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.setdefault(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td>
        <td class="left"  ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>,
           else <var>x</var> (also setting it)</td>
        <td class="center">(5)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.pop(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td>
        <td class="left"  ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>,
           else <var>x</var> (and remove k)</td>
        <td class="center">(8)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.popitem()</code></td>
        <td class="left"  >remove and return an arbitrary (<var>key</var>, <var>value</var>) pair</td>
        <td class="center">(6)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.iteritems()</code></td>
        <td class="left"  >return an iterator over (<var>key</var>, <var>value</var>) pairs</td>
        <td class="center">(2), (3)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.iterkeys()</code></td>
        <td class="left"  >return an iterator over the mapping's keys</td>
        <td class="center">(2), (3)</td></tr>
    <tr><td class="center" valign="baseline"><code><var>a</var>.itervalues()</code></td>
        <td class="left"  >return an iterator over the mapping's values</td>
        <td class="center">(2), (3)</td></tr></tbody>
</table></div>

<p>
Notes:
<dl>
<dt><strong>(1)</strong></dt>
<dd>Raises a <tt class="exception">KeyError</tt> exception if <var>k</var> is not
in the map.

<p>
</dd>
<dt><strong>(2)</strong></dt>
<dd>
<span class="versionnote">New in version 2.2.</span>

<p>
</dd>
<dt><strong>(3)</strong></dt>
<dd>Keys and values are listed in an arbitrary order which is
non-random, varies across Python implementations, and depends on the
dictionary's history of insertions and deletions.
If <tt class="method">items()</tt>, <tt class="method">keys()</tt>, <tt class="method">values()</tt>,
<tt class="method">iteritems()</tt>, <tt class="method">iterkeys()</tt>, and <tt class="method">itervalues()</tt>
are called with no intervening modifications to the dictionary, the
lists will directly correspond.  This allows the creation of
<code>(<var>value</var>, <var>key</var>)</code> pairs using <tt class="function">zip()</tt>:
"<tt class="samp">pairs = zip(<var>a</var>.values(), <var>a</var>.keys())</tt>".  The same
relationship holds for the <tt class="method">iterkeys()</tt> and
<tt class="method">itervalues()</tt> methods: "<tt class="samp">pairs = zip(<var>a</var>.itervalues(),
<var>a</var>.iterkeys())</tt>" provides the same value for <code>pairs</code>.
Another way to create the same list is "<tt class="samp">pairs = [(v, k) for (k,
v) in <var>a</var>.iteritems()]</tt>".

<p>
</dd>
<dt><strong>(4)</strong></dt>
<dd>Never raises an exception if <var>k</var> is not in the map,
instead it returns <var>x</var>.  <var>x</var> is optional; when <var>x</var> is not
provided and <var>k</var> is not in the map, <code>None</code> is returned.

<p>
</dd>
<dt><strong>(5)</strong></dt>
<dd><tt class="function">setdefault()</tt> is like <tt class="function">get()</tt>, except
that if <var>k</var> is missing, <var>x</var> is both returned and inserted into
the dictionary as the value of <var>k</var>. <var>x</var> defaults to <var>None</var>.

<p>
</dd>
<dt><strong>(6)</strong></dt>
<dd><tt class="function">popitem()</tt> is useful to destructively iterate
over a dictionary, as often used in set algorithms.  If the dictionary
is empty, calling <tt class="function">popitem()</tt> raises a <tt class="exception">KeyError</tt>.

<p>
</dd>
<dt><strong>(7)</strong></dt>
<dd><tt class="function">fromkeys()</tt> is a class method that returns a
new dictionary. <var>value</var> defaults to <code>None</code>.  
<span class="versionnote">New in version 2.3.</span>

<p>
</dd>
<dt><strong>(8)</strong></dt>
<dd><tt class="function">pop()</tt> raises a <tt class="exception">KeyError</tt> when no default
value is given and the key is not found.  
<span class="versionnote">New in version 2.3.</span>

<p>
</dd>
<dt><strong>(9)</strong></dt>
<dd><tt class="function">update()</tt> accepts either another mapping object
or an iterable of key/value pairs (as a tuple or other iterable of
length two).  If keyword arguments are specified, the mapping is
then is updated with those key/value pairs:
"<tt class="samp">d.update(red=1, blue=2)</tt>".

<span class="versionnote">Changed in version 2.4:
Allowed the argument to be an iterable of key/value
                pairs and allowed keyword arguments.</span>

<p>
</dd>
<dt><strong>(10)</strong></dt>
<dd>If a subclass of dict defines a method <tt class="method">__missing__</tt>,
if the key <var>k</var> is not present, the <var>a</var>[<var>k</var>] operation calls
that method with the key <var>k</var> as argument.  The <var>a</var>[<var>k</var>]
operation then returns or raises whatever is returned or raised by the
<tt class="function">__missing__</tt>(<var>k</var>) call if the key is not present.
No other operations or methods invoke <tt class="method">__missing__</tt>().
If <tt class="method">__missing__</tt> is not defined, <tt class="exception">KeyError</tt> is raised.
<tt class="method">__missing__</tt> must be a method; it cannot be an instance variable.
For an example, see <tt class="module">collections</tt>.<tt class="class">defaultdict</tt>.

<span class="versionnote">New in version 2.5.</span>

<p>
</dd>
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.7 set Types "
  href="types-set.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="3. built-in Types"
  href="types.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="3.9 file Objects"
  href="bltin-file-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="types-set.html">3.7 Set Types </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="types.html">3. Built-in Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bltin-file-objects.html">3.9 File Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
